<template>
  <div>
     <el-header>
        
        <router-link >
        
         <img
            src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
            style="height: 60%; margin-top: 10px ;margin: 0 auto;"
          />
          
        </router-link>
        
      </el-header>
    <div class="m-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="ruleForm.realname"></el-input>
        </el-form-item>
       <el-form-item label="手机号" prop="phone">
          <el-input  v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="idcard">
          <el-input  v-model="ruleForm.idcard"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="ruleForm.address"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="danger" v-if="ruleForm.id" @click="deleteadd()">删除信息</el-button>
        </el-form-item>
         
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Header from "../components/Header";
export default {
  //import 引入的组件需要注入到对象中才能使用
  name: "BlogEdit.vue",
  components: { Header },
  data() {
    return {
      ruleForm: {
        id: "",
        title: "",
        description: "",
        content: "",
        address:"",
        idcard:"",
        phone:"",
        realname:"",
        userid:""
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  methods: {
   submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              
            const _this = this
            console.log(_this.ruleForm.address)
            this.$axios.post('/address/edit', this.ruleForm, {
              headers: {
                "Authorization": localStorage.getItem("token")
              }
            }).then(res => {
              console.log(res)
              _this.$alert('操作成功', '提示', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push("/blog/address")
                }
              });

            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },deleteadd(){
             const _this = this
             ///address/deleteadd/1
        // console.log(JSON.parse(sessionStorage.getItem("userInfo")).id)
        _this.$axios.get(`/address/deleteadd/`+this.$route.params.blogId,{
              headers: {
                "Authorization": localStorage.getItem("token")//验证权限
              }
            }).then(res => {
          console.log('res',res)
           this.$notify({
      title: "删除成功！",
      message: "删除成功",
      duration: 1500,
    })
          _this.$router.push("/blog/address")
        })
      }
    },
    created() {
      const blogId = this.$route.params.blogId
      console.log("blogId********************************")
      console.log(blogId)
      const _this = this
      if(blogId) {//获取博客的id123213
      ///adressone/{userid}
        this.$axios.get('/address/adressone/' + blogId).then(res => {
          const blog = res.data.data
          console.log(blog)
          _this.ruleForm.id = blog.id
           _this.ruleForm.realname= blog.realname
          _this.ruleForm.title = blog.title
          _this.ruleForm.address = blog.address
          _this.ruleForm.idcard = blog.idcard
          _this.ruleForm.phone = blog.phone
          _this.ruleForm.description = blog.description
          _this.ruleForm.content = blog.content
        })
      }

  },
};
</script>
<style scoped>
.m-content {
  text-align: center;
}
</style>
